package com.dragon.mmochat.message;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatBattleService;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_1_7S extends SkymobiProtocolMessage {
	MmochatPlayer me;
	boolean isRoleUplevel = false;

	public Mmochat_1_7S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_1_7);
	}

	public Mmochat_1_7S(boolean uplevel) {
		isRoleUplevel = uplevel;
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_1_7);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		// 人物是否升级
		if (isRoleUplevel) {
			out.put((byte) 1);
		} else {
			out.put((byte) 0);
		}
		// 人物信息
		byte bloodPercent = (byte) (Math.min(100, me.getCurBlood() * 100
				/ Math.max(1, me.getTotalBlood())));
		out.put(bloodPercent);
		byte spritPercent = (byte) (Math.min(100, me.getCurSprit() * 100
				/ Math.max(1, me.getTotalSprit())));
		out.put(spritPercent);
		byte expPercent = (byte) (Math.min(100, me.getExp()
				* 100
				/ Math.max(1, MmochatBattleService.getHumanExpNeedToUplevel(me
						.getLevel(), me.getLife_turn()))));
		out.put(expPercent);

		// 宠物信息
		MmochatPet pet = me.getPkPet();
		if (pet == null) {
			out.putInt(-1); // petId
			out.putInt(0);
			out.putInt(0);
			out.putInt(0);
		} else {
			out.putInt(pet.getPetId());
			out.putInt(pet.getCurBlood());
			out.putInt(pet.getCurSprit());
			out.putInt(pet.getExp());
		}
	}

	public void setMe(MmochatPlayer me) {
		this.me = me;
	}

	public void setRoleUplevel(boolean isRoleUplevel) {
		this.isRoleUplevel = isRoleUplevel;
	}

}